// @ts-nocheck
const vscode = acquireVsCodeApi();

$(function () {
  // 页面加载时需要获取是否登录，已登录不显示登录页面，未登录显示登录页面
  vscode.postMessage({ command: 'isGiteeLogin' });
  bindClick();
  window.removeEventListener('message', messageListener);
  window.addEventListener('message', messageListener);
});
// 显示隐藏密码图标设置
$('#password-icon').mousedown(function () {
  $('#password').attr('type', 'text');
  $('#password-icon').attr('class', 'codicon codicon-eye');
});
$('#password-icon').mouseup(function () {
  $('#password').attr('type', 'password');
  $('#password-icon').attr('class', 'codicon codicon-eye-closed');
});
$('#password-icon').mouseleave(function () {
  $('#password').attr('type', 'password');
  $('#password-icon').attr('class', 'codicon codicon-eye-closed');
});
// 按下回车键，提交登录数据
$(document).keyup(function (event) {
  if (event.keyCode === 13) {
    $('#btn-gitee').trigger('click');
  }
});
// 登录gitee
function toLogin() {
  $('#btn-gitee').on('click', () => {
    const userNum = $('#user-num').val().trim();
    const password = $('#password').val().trim();
    // 信息为空，不允许登录
    if (userNum === '' || password === '') {
      $('#gitee-msg').addClass('content-block').removeClass('content-hide');
      $('#gitee-msg-incorrect-input').css('display', 'none');
      return;
    }
    vscode.postMessage({
      command: 'handleLogin',
      paramObj: {
        userNum,
        password,
      },
    });
  });
}
// 注销登录gitee
function cancelLogin() {
  $('#gitee-cancel').on('click', () => {
    $('input').val('');
    $('#gitee-msg-incorrect-input').css('display', 'none');
    $('#gitee-msg').addClass('content-hide').removeClass('content-block');
    const userNum = $('#logined-num').html().trim();
    vscode.postMessage({ command: 'cancelLogin', paramObj: { userNum } });
  });
}
// 监听用户输入
function changeInput() {
  $('#password').bind('input propertychange', function () {
    const currentInputValue = $(this).val();
    if (currentInputValue) {
      $('#gitee-msg').addClass('content-hide').removeClass('content-block');
    } else {
      $('#gitee-msg-incorrect-input').css('display', 'none');
    }
  });
}
// 处理事件
function bindClick() {
  toLogin();
  cancelLogin();
  changeInput();
}
// 返回登录结果
function returnIsGiteeLogin(result) {
  if (result.isLogin) {
    const userInfo = result.userInfo.giteeUserInfo;
    $('#logined-num').html(userInfo.name);
    $('#user-nickname').html(userInfo.login);
    $('#user-avatar').attr('src', userInfo.avatar_url);
    $('#email').html(userInfo.email);
    $('#has-login').addClass('content-block').removeClass('content-hide');
    $('#gitee-login').addClass('content-hide').removeClass('content-block');
  } else {
    $('#user-num').attr({ 'value': result.email });
    $('#gitee-login').addClass('content-block').removeClass('content-hide');
    $('#has-login').addClass('content-hide').removeClass('content-block');
  }
}
// 返回邮箱或密码错误
function incorrectUsernameOrPassword(result) {
  if (result.isIncorrectUsernameOrPassword) {
    $('#gitee-msg').addClass('content-hide').removeClass('content-block');
    $('#gitee-msg-incorrect-input').css('display', 'block');
  } else {
    $('#gitee-msg-incorrect-input').css('display', 'none');
  }
}
const messageListener = (event) => {
  const message = event.data;
  switch (message.command) {
    case 'returnIsGiteeLogin':
      returnIsGiteeLogin(message.data);
      break;
    case 'incorrectUsernameOrPassword':
      incorrectUsernameOrPassword(message.data);
      break;
    default:
      break;
  }
};